/*
 * Ability.cpp
 *
 *  Created on: 2014. 7. 9.
 *      Author: mega
 */


#include "../User.h"
#include "Equip.h"
#include "Buff.h"

#include "BattleAbility.h"


namespace ProjectZ { namespace NLogic {

BattleAbility::BattleAbility(User * pUser) : _pUser(pUser)
{
	_pClientCharacterInfo = new CNetCMDDefaultUserInfo;
	_pClientPlayer = new CMvPlayer;
//	_pClientPlayer = (CMvPlayer *)CreatePlayer(_pClientCharacterInfo);

	_nAttackAbility = 0;
}

BattleAbility::~BattleAbility()
{
	delete _pClientCharacterInfo;
	delete _pClientPlayer;
}

void
BattleAbility::Initialize()
{
	_pClientCharacterInfo->Initial();
	_nAttackAbility = 0;
}

void
BattleAbility::Finalize()
{
}

void
BattleAbility::SetUserInfo()
{

	// 캐릭터 기본 정보
	{
		SavePlayerData * pData = _pClientCharacterInfo->GetDefaultPlayerDataPtr();
		if (NULL == pData)
		{
			DEBUGLOG("%s : SavePlayerData is NULL", __FUNCTION__);
			return;
		}

		pData->m_byOpenActiveSkillNum	= 0;
		pData->m_sLevel				= _pUser->GetCharacterInfo().level;
		pData->m_byCharClassType	= _pUser->GetCharacterInfo().classtype;

		pData->m_aStat[0]			= _pUser->GetCharacterInfo().str;
		pData->m_aStat[1]			= _pUser->GetCharacterInfo().dex;
		pData->m_aStat[2]			= _pUser->GetCharacterInfo().con;
		pData->m_aStat[3]			= _pUser->GetCharacterInfo().spi;

		int iSkillStartIndex = _pUser->GetCharacterInfo().classtype * MAX_SKILL;

		if (CLASS_TYPE_PALADIN == _pUser->GetCharacterInfo().classtype) // 성기사 스킬
		{
			iSkillStartIndex = 481;
		}

		for( int i=0 ; i<MAX_SKILL ; i++ )
		{
			pData->m_sSkillTID[i] = (i + iSkillStartIndex);
			pData->m_bySkillLevel[i] = _pUser->GetCharacterInfo().array_Skill[i];
		}
	}


	// 아이템 장착 정보
	{
		NLogic::Equip * pEquip = _pUser->GetEquip();
		if (NULL == pEquip)
		{
			DEBUGLOG("%s : pEquip is NULL", __FUNCTION__);
			return;
		}

		for(int i = 0; i < static_cast<int>(MAX_ITEM_EQUIP_POS); ++i)
		{
			NLogic::Item * pItem = pEquip->GetItem(static_cast<EnumItemEquipPosition>(i));
			if (NULL != pItem)
			{
				stSaveItemData * pEquipItem = _pClientCharacterInfo->GetEquip(static_cast<int>(pItem->GetEquipPos()));

				pEquipItem->m_lItemSeq			= pItem->GetItemSeq();
				pEquipItem->m_byCurDurability	= pItem->GetData().cur_duration;
				pEquipItem->m_byMaxDurability	= pItem->GetData().max_duration;
				pEquipItem->m_byCount			= pItem->GetData().quantity;
				pEquipItem->m_chSettedType		= pItem->GetData().set_type;
				pEquipItem->m_bNonIdentify		= pItem->GetData().non_identity;
				pEquipItem->m_byRefineStep		= pItem->GetData().cur_refine_step;
				pEquipItem->m_eQuality			= pItem->GetData().quality;
				pEquipItem->m_sLevel			= pItem->GetData().level;


				for (int i = 0; i < 7; i++)
				{
					pEquipItem->m_aItemEffect[i].SetType(pItem->GetData().eff_type[i]);
					pEquipItem->m_aItemEffect[i].SetPos(pItem->GetData().eff_pos[i]);
					pEquipItem->m_aItemEffect[i].SetValue(pItem->GetData().eff_value[i]);
				}

				pEquipItem->m_byOpendUpgradeStoneSlotCount	= pItem->GetData().open_upgrade_stone_slot;
				pEquipItem->m_byBaseAblityEnhanceRate		= pItem->GetData().ability_enhance_rate;
				pEquipItem->m_byRefineStepMax				= pItem->GetData().max_enhance_step;
				pEquipItem->m_byBelongItem					= pItem->GetData().buy_use;
				pEquipItem->m_byEvolveStep					= pItem->GetData().evolve_step;
				pEquipItem->m_byEvolveMaxStep				= pItem->GetData().evolve_max;
				pEquipItem->m_sEvolvePoint					= pItem->GetData().evolve_point;
				pEquipItem->m_byEvolveEffect				= pItem->GetData().evolve_percent;
				pEquipItem->m_sEvolveEffectValue			= pItem->GetData().evolve_value;

				pEquipItem->m_stItemSeq.m_byClassType		= pItem->GetData().class_type;
				pEquipItem->m_stItemSeq.m_bySheetType		= pItem->GetData().sub_type;
				pEquipItem->m_stItemSeq.m_sTID				= pItem->GetData().tid;

			}
		}
	}

	// 스킬 버프
	{
		CNetCMDSkillBuff * pSkillBuff = _pClientCharacterInfo->GetSkillBuffPtr();
		if (NULL == pSkillBuff)
		{
			DEBUGLOG("%s : pSkillBuff is NULL");
			return;
		}

		NLogic::Buff * pBuff = _pUser->GetBuff();
		if (NULL == pBuff)
		{
			DEBUGLOG("%s : pBuff is NULL");
			return;
		}

		pBuff->RefleshVIPBuff();

		std::time_t t = std::time(0);  // t is an integer type
		unsigned int nTime = t;

		std::for_each(pBuff->GetDataVector().begin(), pBuff->GetDataVector().end(), [&](NLogic::Buff::Data * pData) {

			if (pData->_regdate < nTime)
				pData->Clear();

			pSkillBuff->s16skill_tid[pData->u8type]		= pData->u16skill_tid;
			pSkillBuff->s16skill_value[pData->u8type]	= pData->s16skill_value;
			pSkillBuff->u8skill_level[pData->u8type]	= pData->u8skill_level;

			if (30000 > pData->_regdate - nTime)
				pSkillBuff->u16skill_time[pData->u8type]	= (pData->_regdate - nTime);
			else
				pSkillBuff->u16skill_time[pData->u8type]	= 30000;
		} );
	}


	// 페어리 정보
	{
		CNetCMDFairyInfo * pFairyInfo = _pClientCharacterInfo->GetFairyInfoPtr();
		if (NULL == pFairyInfo)
		{
			DEBUGLOG("%s : pFairyInfo is NULL", __FUNCTION__);
			return;
		}

		NLogic::Equip * pEquip = _pUser->GetEquip();
		if (NULL == pEquip)
		{
			DEBUGLOG("%s : pEquip is NULL", __FUNCTION__);
			return;
		}

		NLogic::Item * pItem = pEquip->GetItem(ITEM_EQUIP_POS_FAIRY);
		if (NULL != pItem)
		{
			pFairyInfo->tFairyEquipInfo.m_lItemSeq			= pItem->GetItemSeq();
			pFairyInfo->tFairyEquipInfo.m_byCurDurability	= pItem->GetData().cur_duration;
			pFairyInfo->tFairyEquipInfo.m_byMaxDurability	= pItem->GetData().max_duration;
			pFairyInfo->tFairyEquipInfo.m_byCount			= pItem->GetData().quantity;
			pFairyInfo->tFairyEquipInfo.m_chSettedType		= pItem->GetData().set_type;
			pFairyInfo->tFairyEquipInfo.m_bNonIdentify		= pItem->GetData().non_identity;
			pFairyInfo->tFairyEquipInfo.m_byRefineStep		= pItem->GetData().cur_refine_step;
			pFairyInfo->tFairyEquipInfo.m_eQuality			= pItem->GetData().quality;
			pFairyInfo->tFairyEquipInfo.m_sLevel			= pItem->GetData().level;


			for (int i = 0; i < 7; i++)
			{
				pFairyInfo->tFairyEquipInfo.m_aItemEffect[i].SetType(pItem->GetData().eff_type[i]);
				pFairyInfo->tFairyEquipInfo.m_aItemEffect[i].SetPos(pItem->GetData().eff_pos[i]);
				pFairyInfo->tFairyEquipInfo.m_aItemEffect[i].SetValue(pItem->GetData().eff_value[i]);
			}

			pFairyInfo->tFairyEquipInfo.m_byOpendUpgradeStoneSlotCount	= pItem->GetData().open_upgrade_stone_slot;
			pFairyInfo->tFairyEquipInfo.m_byBaseAblityEnhanceRate		= pItem->GetData().ability_enhance_rate;
			pFairyInfo->tFairyEquipInfo.m_byRefineStepMax				= pItem->GetData().max_enhance_step;
			pFairyInfo->tFairyEquipInfo.m_byBelongItem					= pItem->GetData().buy_use;
			pFairyInfo->tFairyEquipInfo.m_byEvolveStep					= pItem->GetData().evolve_step;
			pFairyInfo->tFairyEquipInfo.m_byEvolveMaxStep				= pItem->GetData().evolve_max;
			pFairyInfo->tFairyEquipInfo.m_sEvolvePoint					= pItem->GetData().evolve_point;
			pFairyInfo->tFairyEquipInfo.m_byEvolveEffect				= pItem->GetData().evolve_percent;
			pFairyInfo->tFairyEquipInfo.m_sEvolveEffectValue			= pItem->GetData().evolve_value;

			pFairyInfo->tFairyEquipInfo.m_stItemSeq.m_byClassType		= pItem->GetData().class_type;
			pFairyInfo->tFairyEquipInfo.m_stItemSeq.m_bySheetType		= pItem->GetData().sub_type;
			pFairyInfo->tFairyEquipInfo.m_stItemSeq.m_sTID				= pItem->GetData().tid;
		}
	}



	_pClientPlayer->SetDefaultPlayerInfo(_pClientCharacterInfo);


	// 전투 능력 계산
	_nAttackAbility = _pClientPlayer->GetStatTotal(EnumStat::STAT_SUB_ATTACK);
	DEBUGLOG("%s : Class=%d, Level=%d, Attack=%d", __FUNCTION__, _pUser->GetCharacterInfo().classtype, _pUser->GetCharacterInfo().level, _nAttackAbility);
}

int
BattleAbility::GetAttackAbility()
{
	return _nAttackAbility;
}

} /* namespace NLogic */ } /* namespace ProjectZ */
